package com.auemall.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class SecurityUtils {

    final static String salt = "catosoftTarchcloudcatosoft";

    public static String getSHA(String inStr){
    	
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(inStr.getBytes("UTF-8"));
            return byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public static String encodePassword(String password){
        return getSHA(salt+getSHA(password)+salt);
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
    
    public static void main(String [] args){
        String oldPwd = "Abd123e";
        String newPwd = "Abd123e";
        System.out.println(encodePassword(oldPwd));
        System.out.println(encodePassword(newPwd));
    }
}
